package com.lxw.study.behavioral.command.case3;

public class DeleteTextCommand implements Command {
    private String deletedText;
    private StringBuilder textEditor;
    private int position;

    public DeleteTextCommand(StringBuilder editor, int position, int length) {
        this.textEditor = editor;
        this.position = position;
        this.deletedText = textEditor.substring(position, position + length);
    }

    @Override
    public void execute() {
        textEditor.delete(position, position + deletedText.length());
    }

    @Override
    public void undo() {
        textEditor.insert(position, deletedText);
    }
}
